#--
# Copyright (c) 2007 Maximilian Ott
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#++
module BrowseHelper
  
  # Overide the follwing methods
  #
  def show_main(rec)
    raise 'Implement "show_main" in browse_helper'
  end
  
  def show_meta(rec)
    raise 'Implement "show_meta" in browse_helper'
  end
  
  def show_modes
    a = []
    BrowseController::modes.each do |mode, name|
      if name.nil?
        name = mode.to_s.capitalize
      end
      if @mode == mode
        a <<  name
      else
        a << link_to(name, {:action => 'browse', :mode => mode})
      end
    end
    a.join ' | '    
  end
  
  def default_result_header
    name = nil
    BrowseController::modes.find do |mode, human| 
      if mode == @mode 
        name = human ? human : mode.capitalize
      end
    end
    @_markaby.h2 name
  end
  
  def order_by_selector()
    #BrowseController::sort_options
    i = -1
    list = BrowseController::ORDER_OPTIONS.collect do |name, sql|
      i += 1
      [name, i]
    end
    #list = [["1", "one"], ["2", "two"]]
    
    id = 'order_selector'
    @_markaby.select.filter(:name => 'order_by', :id => id) do 
      selected = session[:list_order] || 0
      options_for_select(list, selected)
    end
    @_markaby << javascript_tag("ListOrder.observe('#{id}');")
  end
  

  ########################
  
  def text_selector(fe, pos, prefix, opts = {})
    #text_field :f, :text_val, :index => pos, :size => 10, :value => fe.text_val
    id = "f_#{pos}_val"
    o = {:name => prefix + "[text_val]", :id => id, :value => fe.text_val,
              :type => "text", :size => 10}
    o.merge! opts
    @_markaby.input o
    @_markaby << javascript_tag("ListFilter.observe('#{id}');")
  end
  
  def time_span_selector(fe, pos, prefix, opts = {})
    today = Date.today
    from = fe.from || Date.civil(today.year, today.month)
    to = fe.to || Date.civil(today.year, today.month, -1) # -1 .. last day
    @_markaby.text ' between '        
    _date_selector :from, from, pos
    @_markaby.text ' and '
    _date_selector :to, to, pos, true
  end
  
  def _date_selector(name, date = Date.today, i = 1, is_to = false)
    @_markaby.input :type => "hidden", :value => (is_to ? -1 : 1), :name => "f[#{i}][#{name}(3i)]"
    @_markaby << select_month(date, :prefix => "f[#{i}][#{name}(2i)]", :discard_type => 1) # :include_blank => 1
    @_markaby << select_year(date, :prefix => "f[#{i}][#{name}(1i)]", :discard_type => 1, :start_year => 2002, :end_year => Time.now.year + 5) #, :include_blank => 1
    @_markaby << javascript_tag("ListFilter.observe('f_#{i}_#{name}_1i'); ListFilter.observe('f_#{i}_#{name}_2i');")
  end

  def list_selector(list, fe, pos, prefix, opts = {})
    id = "f_#{pos}_val"
    name = prefix + "[#{opts[:type] || "int_val"}]"
    @_markaby.select.filter(:name => name, :id => id) do 
      selected = opts[:selected] || fe.value || -1
      puts "FE-SELECTED>> #{selected} : #{fe.inspect}"
      #selected = fe.int_val || -1
      options_for_select(list, selected)
    end
    @_markaby << javascript_tag("ListFilter.observe('#{id}');")
  end

  def render_filter_element fe, index, prefix
    fname = "#{fe.field_name}_selector"
    puts "FFF> Checking for '#{fname}'"
    if self.respond_to? fname
      self.send(fname, fe, index, prefix)
    else 
      tname = "#{fe.argument_type}_selector"  
      puts "FFF> Checking for '#{tname}'"
      if self.respond_to? tname
        self.send(tname, fe, index, prefix)
      else
        raise "Unsupported filter element '#{fname}:#{tname}' (#{fe.inspect})"
      end
    end
  end

#  def build_filter(filters)
#    i = 1
#    @_markaby.text 'Show all '
#    filters.each do |f|
#      @_markaby.text " #{f.op} " if i > 1
#      prefix = "f[#{i}]"
#      @_markaby.input(:name => "#{prefix}[op]", :value => f.op || 'and', :type => "hidden")
#      js = "$('filterSpinner').show();$('list_filter_form').hide().submit(); return false;"
#      @_markaby.select.filter(:name => prefix + "[ftype]", :onchange => js) do 
#        puts "FFF>> #{f.inspect}"
#        options_for_select(f.types, f.ftype)
#      end
#      
#      @_markaby.input(:name => "#{prefix}[arg_type]", :value => f.ftype || 'x', :type => "hidden")
#      
#      n = f.field_name
#      unless n == ''
##      puts ">>>> RESPOND #{respond_to? 'text_selector'}"
#        puts "Filed name: #{n}:#{n.class}"
#        @_markaby.text '  '
#        unless n.nil? || f.to_s.empty?
#          fname = "#{n}_selector"
#          puts "FFF> Checking for '#{fname}'"
#          if self.respond_to? fname
#            self.send(fname, f, i, prefix)
#          else 
#            tname = "#{f.argument_type}_selector"  
#            puts "FFF> Checking for '#{tname}'"
#            if self.respond_to? tname
#              self.send(tname, f, i, prefix)
#            else
#              raise "Unsupported filter element '#{f.inspect}'"
#            end
#          end
#          @_markaby.text '  '
#        end
#      end
#      i += 1
#    end
#    nil
#  end
  
end
